package com.gzy.xuexiziliao.controller;


import com.gzy.xuexiziliao.common.result.PageResult;
import com.gzy.xuexiziliao.common.result.Result;
import com.gzy.xuexiziliao.pojo.entity.Information;
import com.gzy.xuexiziliao.service.InformationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 资讯管理
 */
@RestController
@Slf4j
@RequestMapping("admin/information")
public class InformationController {

    @Autowired
    private InformationService informationService;
    @PostMapping("/save")
    public Result<Information> save(@RequestBody Information information){
    log.info("新增资讯,information:{}",information);
    Information infor = informationService.save(information);
    return Result.success(infor);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Information information){
    log.info("修改资讯详情,information:{}",information);
     informationService.update(information);
    return Result.success("修改成功");
    }

    @DeleteMapping("/delete/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
    log.info("删除资讯,ids:{}",ids);
    informationService.delete(ids);
    return Result.success("删除成功");
    }

    @GetMapping("/{categoryId}")
    public Result<List<Information>> getById(@PathVariable Integer categoryId){
    log.info("根据分类id查询资讯,categoryId:{}",categoryId);
        List<Information> informationList = informationService.getBycategoryId(categoryId);
        if (informationList!=null){
            return Result.success(informationList);
        }
        return Result.error("未找到该分类下的资讯");
    }

    @GetMapping("/list")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end){
        log.info("分页查询参数：{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        PageResult pageResult=informationService.page(page,pageSize,name,gender,begin,end);
        return Result.success(pageResult);
    }

    @PostMapping("/getBYTitle")
    public Result<Information> getBYTitle(@RequestBody Information information){
        log.info("根据标题查询资讯,title:{}",information);
        Information byTitle = informationService.getBYTitle(information);
        if (byTitle ==null){
            return Result.error("未找到该资讯");
        }
        return Result.success(byTitle);
    }
    @GetMapping("/getBYInformationId/{id}")
    public Result<Information> getBYInformationId(@PathVariable Integer id){
        log.info("根据id查询资讯,id:{}",id);
        Information byInformationId = informationService.getBYInformationId(id);
        if (byInformationId ==null){
            return Result.error("未找到该资讯");
        }
        return Result.success(byInformationId);
    }
}
